/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mabi.frontend.appbuilder.navigation;

import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.MenuItem;
import java.beans.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Justo Disla
 */
public class NavigationManager implements Serializable {

    public static final String NAVIGATION_CHANGED = "NAVIGATION_CHANGED";
    private static NavigationManager instance;
    private PropertyChangeSupport propertySupport;

    public static NavigationManager getDefault() {
        if (instance == null) {
            instance = new NavigationManager();
        }
        return instance;
    }
    List<MenuBar.MenuItem> menuItems;

    public synchronized void addMenuItem(MenuItem item) {
        if (!menuItems.contains(item)) {
            menuItems.add(item);
            propertySupport.firePropertyChange(NAVIGATION_CHANGED, 0, 1);
        }
    }

    public synchronized void removeMenuItem(MenuItem item) {
        if (menuItems.contains(item)) {
            menuItems.remove(item);
            propertySupport.firePropertyChange(NAVIGATION_CHANGED, 0, 1);
        }
    }

    public synchronized void loadMenuItems(List<MenuItem> items) {
        menuItems.addAll(items);
        propertySupport.firePropertyChange(NAVIGATION_CHANGED, 0, 1);
    }

    public NavigationManager() {
        propertySupport = new PropertyChangeSupport(this);
        menuItems = new ArrayList();
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }
}
